//
//  settleDeliveryUpCell.m
//  ProjectforHDX
//
//  Created by motom on 16/8/10.
//  Copyright © 2016年 H.DX. All rights reserved.
//

#import "settleDeliveryUpCell.h"
#import "SettleDeliverPlate.h"

@protocol settleDeliveryUpcellDelegate <NSObject>

-(void)btnSelect:(NSInteger)tag;

@end

@interface settleDeliveryUpCell()


//!板件名
@property (nonatomic, strong) RZCustomLable *plateName;
@property(nonatomic,strong)RZCustomLable *plateNameText;
//!板件号
@property (nonatomic, strong) RZCustomLable *plateNum;
@property(nonatomic,strong)RZCustomLable *plateNumText;

//!版本号
@property (nonatomic, strong) RZCustomLable *plateVersionNum;
@property(nonatomic,strong)RZCustomLable *plateVersionNumText;

//!货权
@property (nonatomic, strong) RZCustomLable *plateCompany;
@property(nonatomic,strong)RZCustomLable *plateCompanyText;

//!序列号
@property (nonatomic, strong) RZCustomLable *plateSerialNum;
@property(nonatomic,strong)RZCustomLable *plateSerialNumText;

@property(nonatomic,assign)BOOL isSelect;

@end

@implementation settleDeliveryUpCell



-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupView];
    }
    return self;
}
#define CELL_HEIGHT 70
//初始化控件
-(void)setupView
{
    self.editing = NO;
    self.containView = [[UIView alloc]init];
    self.containView.clipsToBounds = YES;
    //    self.containView.backgroundColor = [UIColor orangeColor];
    
    [self addSubview:self.containView];
    //    self.containView.backgroundColor = [ UIColor whiteColor];
    
    
    self.plateName = [[RZCustomLable alloc]init];
    [self.plateName setFont:CustomFont(13)];
    self.plateName.text = [NSString stringWithFormat:@"%@: ",[UtilCheck language:@"setting.plateName"]];
    [self.plateName setTextColor:[UtilCheck hexStringToColor:@"9d9fa1" alpha:1]];
    [self.containView addSubview:self.plateName];
    
    self.plateNum = [[RZCustomLable alloc]init];
    [self.plateNum setFont:CustomFont(13)];
    self.plateNum.text  = [NSString stringWithFormat:@"%@: ",[UtilCheck language:@"setting.plateNO"]];
    [self.plateNum setTextColor:[UtilCheck hexStringToColor:@"9d9fa1" alpha:1]];
    [self.containView addSubview:self.plateNum];
    
    self.plateSerialNum = [[RZCustomLable alloc]init];
    [self.plateSerialNum setFont:CustomFont(13)];
    self.plateSerialNum.text  = [NSString stringWithFormat:@"%@: ",[UtilCheck language:@"setting.plateSerial"]];
    [self.plateSerialNum setTextColor:[UtilCheck hexStringToColor:@"9d9fa1" alpha:1]];
    [self.containView addSubview:self.plateSerialNum];
    
    self.plateVersionNum = [[RZCustomLable alloc]init];
    [self.plateVersionNum setFont:CustomFont(13)];
    self.plateVersionNum.text  = [NSString stringWithFormat:@"%@: ",[UtilCheck language:@"setting.plateCode"]];
    [self.plateVersionNum setTextColor:[UtilCheck hexStringToColor:@"9d9fa1" alpha:1]];
    [self.containView addSubview:self.plateVersionNum];
    
    self.plateCompany = [[RZCustomLable alloc]init];
    [self.plateCompany setFont:CustomFont(13)];
    self.plateCompany.text  = [NSString stringWithFormat:@"%@: ",[UtilCheck language:@"seeting.goodWeight"]];
    [self.plateCompany setTextColor:[UtilCheck hexStringToColor:@"9d9fa1" alpha:1]];
    [self.containView addSubview:self.plateCompany];
    
    self.alterBtn = [[RZCustomButtom alloc]init];
    self.alterBtn.titleLabel.font= CustomFont(14);
//    [self.alterBtn setTitle:@"修改" forState:UIControlStateNormal];
    [self.alterBtn setImage:BDFILEIMAGENAEM(@"Order/btn_Up_unselected") forState:UIControlStateNormal];
    [self.alterBtn setImage:BDFILEIMAGENAEM(@"Order/btn_Up_selected") forState:UIControlStateSelected];
    [self.alterBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.containView addSubview:self.alterBtn];
    self.alterBtn.right = SCREEN_WIDTH - 30;
    self.alterBtn.top = CELL_HEIGHT/2.f-15;
    [self.alterBtn sizeToFit];
    [self.alterBtn addTarget:self action:@selector(plateSelect:) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)plateSelect:(RZCustomButtom *)btn
{
    if(btn.selected == NO)
    {
        btn.selected = YES;
        self.selectBlock(btn.selected,self.plate);
        if ([self.delegate respondsToSelector:@selector(clearOtherBtn:)]) {
            [self.delegate clearOtherBtn:btn];
        }
    }
    else
    {
        btn.selected = NO;
        self.selectBlock(btn.selected,self.plate);
    }
}

#define lableHDistance 80
#define lableVDistance 3
-(void)layoutSubviews
{
    if (_plate) {
        self.plateName.top = 6;
        self.plateName.left = 10;
        [self.plateName sizeToFit];
        
        
        
        self.plateNum.top = self.plateName.top;
        self.plateNum.left = self.plateName.right + lableHDistance;
        [self.plateNum sizeToFit];
        
        
        CGFloat marW = self.plateNum.left - self.plateName.right;
        CGFloat marW2 = self.alterBtn.left - self.plateNum.right;
        
        //板件名
        CGSize size = [self.plateNameText.text boundingRectWithSize:CGSizeMake(marW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil].size;
        [self.plateNameText setFrame:CGRectMake(self.plateName.right, self.plateName.top, size.width, size.height)];
        
        //板件号
        CGSize size2 = [self.plateNumText.text boundingRectWithSize:CGSizeMake(marW2, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil].size;
        [self.plateNumText setFrame:CGRectMake(self.plateNum.right, self.plateName.top, size2.width, size2.height)];
        
        CGFloat height = 0;
        if (size.height > size2.height) {
            height = self.plateNameText.bottom;
        }
        else{
            height = self.plateNumText.bottom;
        }
        
        self.plateVersionNum.top = height + lableVDistance;
        self.plateVersionNum.left = self.plateName.left;
        [self.plateVersionNum sizeToFit];
        
        self.plateSerialNum.top = self.plateVersionNum.top;
        self.plateSerialNum.left = self.plateNum.left;
        [self.plateSerialNum sizeToFit];
        
        //        //版本号
        CGSize size3 = [self.plateVersionNumText.text boundingRectWithSize:CGSizeMake(marW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil].size;
        [self.plateVersionNumText setFrame:CGRectMake(self.plateVersionNum.right, self.plateVersionNum.top, size3.width, size3.height)];
        //
        //序列号
        CGSize size4 = [self.plateSerialNumText.text boundingRectWithSize:CGSizeMake(marW2, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil].size;
        [self.plateSerialNumText setFrame:CGRectMake(self.plateSerialNum.right, self.plateVersionNum.top, size4.width, size4.height)];
        //
//        CGFloat height2 = 0;
        if (size3.height > size4.height) {
            height = self.plateVersionNumText.bottom;
        }
        else{
            height = self.plateSerialNumText.bottom;
        }
        
        
        self.plateCompany.top = height +lableVDistance;
        self.plateCompany.left = self.plateName.left;
        [self.plateCompany sizeToFit];
        
        //序列号
        CGSize size5 = [self.plateCompanyText.text boundingRectWithSize:CGSizeMake(marW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil].size;
        [self.plateCompanyText setFrame:CGRectMake(self.plateCompany.right, self.plateCompany.top, size5.width, size5.height)];
        //
        
        
        self.cellHeight = self.plateCompanyText.bottom +10;
        
        
        
    }
    else
    {
        self.plateName.top = 6;
        self.plateName.left = 10;
        [self.plateName sizeToFit];
        
        self.plateNum.top = self.plateName.top;
        self.plateNum.left = self.plateName.right + lableHDistance;
        [self.plateNum sizeToFit];
        
        self.plateVersionNum.top = self.plateName.bottom + lableVDistance;
        self.plateVersionNum.left = self.plateName.left;
        [self.plateVersionNum sizeToFit];
        
        self.plateSerialNum.top = self.plateVersionNum.top;
        self.plateSerialNum.left = self.plateNum.left;
        [self.plateSerialNum sizeToFit];
        
        self.plateCompany.top = self.plateVersionNum.bottom +lableVDistance;
        self.plateCompany.left = self.plateName.left;
        [self.plateCompany sizeToFit];
        
    }

    
    
}


- (void)awakeFromNib {
    // Initialization code
}

-(void)setPlateModel:(SettleDeliverPlate *)plate
{

    if (_plate) {
        self.alterBtn.selected = NO;
        self.plateVersionNumText.text = nil;
        self.plateSerialNumText.text = nil;
        self.plateCompanyText.text = nil;
        self.plateNameText.text = nil;
        self.plateNumText.text = nil;
    }
        _plate = plate;
        
        //板件名
        RZCustomLable *plateName = [[RZCustomLable alloc]init];
//        plateName.numberOfLines = 0;
        plateName.text = plate.partsName;
        self.plateNameText = plateName;
        [self addSubview:plateName];
        
        //板件号
        RZCustomLable *plateNo = [[RZCustomLable alloc]init];
//        plateNo.numberOfLines  = 0;
        plateNo.text = plate.partsCode;
        self.plateNumText = plateNo;
        [self addSubview:plateNo];
        
        //版本号
        RZCustomLable *plateVersion = [[RZCustomLable alloc]init];
//        plateVersion.numberOfLines = 0;
        plateVersion.text = plate.partsVersion;
        self.plateVersionNumText = plateVersion;
        [self addSubview:plateVersion];
        
        //序列号
        RZCustomLable *plateSerialNum = [[RZCustomLable alloc] init];
//        plateSerialNum.numberOfLines = 0;
        plateSerialNum.text = plate.partsSN;
        self.plateSerialNumText = plateSerialNum;
        [self addSubview:plateSerialNum];
        
        //货权
        RZCustomLable *plateCompany = [[RZCustomLable alloc] init];
//        plateCompany.numberOfLines = 0;
        plateCompany.text = plate.attribution;
        self.plateCompanyText = plateCompany;
        [self addSubview:plateCompany];
        
        
        [self layoutSubviews];
    
    [self.containView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.cellHeight)];

    
    [self.containView addSoldLine:1.0 soldcolor:[UtilCheck hexStringToColor:@"e1e1df" alpha:1.0] pointA:CGPointMake(0, self.cellHeight) pointB:CGPointMake(self.containView.width, self.cellHeight)];
    

}

-(NSAttributedString *)setPlateMessage:(NSString *)labelName plateStr:(NSString *)plateName
{
    NSMutableAttributedString *labelStr = nil ;
    NSAttributedString *plateStr= nil;
    labelStr = [[NSMutableAttributedString alloc]initWithString:labelName];
    plateStr = [[NSAttributedString alloc]initWithString:plateName attributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
    [labelStr appendAttributedString:plateStr];
    return labelStr;
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}




@end
